home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-portable.exe / quodlibet-3.3.0-portable / data / bin / chunk.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  5KB  |  182 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''Simple class to read IFF chunks.
  5.  
  6. An IFF chunk (used in formats such as AIFF, TIFF, RMFF (RealMedia File
  7. Format)) has the following structure:
  8.  
  9. +----------------+
  10. | ID (4 bytes)   |
  11. +----------------+
  12. | size (4 bytes) |
  13. +----------------+
  14. | data           |
  15. | ...            |
  16. +----------------+
  17.  
  18. The ID is a 4-byte string which identifies the type of chunk.
  19.  
  20. The size field (a 32-bit value, encoded using big-endian byte order)
  21. gives the size of the whole chunk, including the 8-byte header.
  22.  
  23. Usually an IFF-type file consists of one or more chunks.  The proposed
  24. usage of the Chunk class defined here is to instantiate an instance at
  25. the start of each chunk and read from the instance until it reaches
  26. the end, after which a new instance can be instantiated.  At the end
  27. of the file, creating a new instance will fail with a EOFError
  28. exception.
  29.  
  30. Usage:
  31. while True:
  32.     try:
  33.         chunk = Chunk(file)
  34.     except EOFError:
  35.         break
  36.     chunktype = chunk.getname()
  37.     while True:
  38.         data = chunk.read(nbytes)
  39.         if not data:
  40.             pass
  41.         # do something with data
  42.  
  43. The interface is file-like.  The implemented methods are:
  44. read, close, seek, tell, isatty.
  45. Extra methods are: skip() (called by close, skips to the end of the chunk),
  46. getname() (returns the name (ID) of the chunk)
  47.  
  48. The __init__ method has one required argument, a file-like object
  49. (including a chunk instance), and one optional argument, a flag which
  50. specifies whether or not chunks are aligned on 2-byte boundaries.  The
  51. default is 1, i.e. aligned.
  52. '''
  53.  
  54. class Chunk:
  55.     
  56.     def __init__(self, file, align = True, bigendian = True, inclheader = False):
  57.         import struct as struct
  58.         self.closed = False
  59.         self.align = align
  60.         if bigendian:
  61.             strflag = '>'
  62.         else:
  63.             strflag = '<'
  64.         self.file = file
  65.         self.chunkname = file.read(4)
  66.         if len(self.chunkname) < 4:
  67.             raise EOFError
  68.         
  69.         try:
  70.             self.chunksize = struct.unpack(strflag + 'L', file.read(4))[0]
  71.         except struct.error:
  72.             raise EOFError
  73.  
  74.         if inclheader:
  75.             self.chunksize = self.chunksize - 8
  76.         self.size_read = 0
  77.         
  78.         try:
  79.             self.offset = self.file.tell()
  80.         except (AttributeError, IOError):
  81.             self.seekable = False
  82.  
  83.         self.seekable = True
  84.  
  85.     
  86.     def getname(self):
  87.         '''Return the name (ID) of the current chunk.'''
  88.         return self.chunkname
  89.  
  90.     
  91.     def getsize(self):
  92.         '''Return the size of the current chunk.'''
  93.         return self.chunksize
  94.  
  95.     
  96.     def close(self):
  97.         if not self.closed:
  98.             self.skip()
  99.             self.closed = True
  100.  
  101.     
  102.     def isatty(self):
  103.         if self.closed:
  104.             raise ValueError, 'I/O operation on closed file'
  105.         return False
  106.  
  107.     
  108.     def seek(self, pos, whence = 0):
  109.         '''Seek to specified position into the chunk.
  110.         Default position is 0 (start of chunk).
  111.         If the file is not seekable, this will result in an error.
  112.         '''
  113.         if self.closed:
  114.             raise ValueError, 'I/O operation on closed file'
  115.         if not self.seekable:
  116.             raise IOError, 'cannot seek'
  117.         if whence == 1:
  118.             pos = pos + self.size_read
  119.         elif whence == 2:
  120.             pos = pos + self.chunksize
  121.         if pos < 0 or pos > self.chunksize:
  122.             raise RuntimeError
  123.         self.file.seek(self.offset + pos, 0)
  124.         self.size_read = pos
  125.  
  126.     
  127.     def tell(self):
  128.         if self.closed:
  129.             raise ValueError, 'I/O operation on closed file'
  130.         return self.size_read
  131.  
  132.     
  133.     def read(self, size = -1):
  134.         '''Read at most size bytes from the chunk.
  135.         If size is omitted or negative, read until the end
  136.         of the chunk.
  137.         '''
  138.         if self.closed:
  139.             raise ValueError, 'I/O operation on closed file'
  140.         if self.size_read >= self.chunksize:
  141.             return ''
  142.         if None < 0:
  143.             size = self.chunksize - self.size_read
  144.         if size > self.chunksize - self.size_read:
  145.             size = self.chunksize - self.size_read
  146.         data = self.file.read(size)
  147.         self.size_read = self.size_read + len(data)
  148.         if self.size_read == self.chunksize and self.align and self.chunksize & 1:
  149.             dummy = self.file.read(1)
  150.             self.size_read = self.size_read + len(dummy)
  151.         return data
  152.  
  153.     
  154.     def skip(self):
  155.         '''Skip the rest of the chunk.
  156.         If you are not interested in the contents of the chunk,
  157.         this method should be called so that the file points to
  158.         the start of the next chunk.
  159.         '''
  160.         if self.closed:
  161.             raise ValueError, 'I/O operation on closed file'
  162.         if self.seekable:
  163.             
  164.             try:
  165.                 n = self.chunksize - self.size_read
  166.                 if self.align and self.chunksize & 1:
  167.                     n = n + 1
  168.                 self.file.seek(n, 1)
  169.                 self.size_read = self.size_read + n
  170.                 return None
  171.             except IOError:
  172.                 pass
  173.             
  174.  
  175.         while self.size_read < self.chunksize:
  176.             n = min(8192, self.chunksize - self.size_read)
  177.             dummy = self.read(n)
  178.             if not dummy:
  179.                 raise EOFError
  180.  
  181.  
  182.